package com.hy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.reggie.current.R;
import com.hy.reggie.dto.OrdersDto;
import com.hy.reggie.pojo.Orders;
import com.hy.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 订单
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 分页查询
     *
     * @param ordersDto
     * @return
     * @throws ParseException
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(OrdersDto ordersDto) throws ParseException {
        log.info(ordersDto.toString());
        log.info("num值：{}", ordersDto.getNumber());

        Page<Orders> page = new Page<>(ordersDto.getPage(), ordersDto.getPageSize());

        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ordersDto.getNumber() != null, Orders::getNumber, ordersDto.getNumber());

        if (ordersDto.getBeginTime() != null || ordersDto.getEndTime() != null) {
//            queryWrapper.
            queryWrapper.apply("UNIX_TIMESTAMP(order_time) >= UNIX_TIMESTAMP('" + ordersDto.getBeginTime() + "')");

            queryWrapper.apply("UNIX_TIMESTAMP(order_time) < UNIX_TIMESTAMP('" + ordersDto.getEndTime() + "')");

        }

        ordersService.page(page, queryWrapper);

        return R.success(page);
    }

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("用户下单");
        ordersService.submit(orders);
        return R.success("下单成功");
    }

}
